package baseTest.java8;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.stream.Stream;

/**
 * ClassName:LambdaDemo <br/>
 * description:  <br/>
 * Date:     2019/6/19、9:56 <br/>
 *
 * @author Plank
 */
public class LambdaDemo {

    public static void main(String[] args) {
        //tFInterface();
        //tMethodReference();
        //tStream();
        //tStatistics();
        //tJs();
        tDate();
    }

    public static void tDate() {
// Get the current date and time
        LocalDateTime currentTime = LocalDateTime.now();
        System.out.println("Current DateTime: " + currentTime);

        LocalDate date1 = currentTime.toLocalDate();
        System.out.println("date1: " + date1);

        Month month = currentTime.getMonth();
        int day = currentTime.getDayOfMonth();
        int seconds = currentTime.getSecond();

        System.out.println("Month: " + month +"day: " + day +"seconds: " + seconds);

        LocalDateTime date2 = currentTime.withDayOfMonth(10).withYear(2012);
        System.out.println("date2: " + date2);

        //12 december 2014
        LocalDate date3 = LocalDate.of(2014, Month.DECEMBER, 12);
        System.out.println("date3: " + date3);

        //22 hour 15 minutes
        LocalTime date4 = LocalTime.of(22, 15);
        System.out.println("date4: " + date4);

        //parse a string
        LocalTime date5 = LocalTime.parse("20:15:30");
        System.out.println("date5: " + date5);
    }

    /**
     * @Author Plank
     * @Description //js引擎
     * @Date 11:28 2019/6/19
     * @Param []
     * @return void
     **/
    public static void tJs() {
        ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
        ScriptEngine nashorn = scriptEngineManager.getEngineByName("nashorn");

        String name = "Mahesh";
        Integer result = null;

        try {
            nashorn.eval("print('" + name + "')");
            result = (Integer) nashorn.eval("10 + 2");

        } catch(ScriptException e) {
            System.out.println("Error executing script: "+ e.getMessage());
        }
        System.out.println(result.toString());
    }

    /**
     * @Author Plank
     * @Description //TODO
     * @Date 11:27 2019/6/19
     * @Param []
     * @return void
     **/
    public static void tStatistics() {
        List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);

        IntSummaryStatistics stats = numbers.stream().mapToInt((x) -> x).summaryStatistics();

        System.out.println("Highest number in List : " + stats.getMax());
        System.out.println("Lowest number in List : " + stats.getMin());
        System.out.println("Sum of all numbers : " + stats.getSum());
        System.out.println("Average of all numbers : " + stats.getAverage());
    }

    /**
     * @Author Plank
     * @Description //stream
     * @Date 11:14 2019/6/19
     * @Param []
     * @return void
     **/

    public static void tStream() {
        List<String> strings = Arrays.asList( "", "bc", "efg", "abcd","abc","", "jkl");
        strings.parallelStream().filter(string -> !string.isEmpty()).map( item -> item+item).sorted().limit(1).forEach(System.out::println);
    }
    /**
     * @Author Plank
     * @Description 方法接口
     * @Date 9:59 2019/6/19
     * @Param []
     * @return void
     **/
    public static void tFInterface() {
        final String param = "better";
        FInterface fInterface=() ->System.out.println("good===="+param);
        fInterface.send();
    }

    /**
     * @Author Plank
     * @Description //方法引用
     * @Date 11:02 2019/6/19
     * @Param []
     * @return void
     **/

    public static void tMethodReference() {
        Stream.of(1,2,3,4).forEach(System.out::println);
    }
}
